package cn.wolfcode.wolf2w.business.service.Impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyEs;
import cn.wolfcode.wolf2w.business.repository.StrategyEsRepository;
import cn.wolfcode.wolf2w.business.service.IMessageService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import com.alibaba.fastjson2.JSON;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private StrategyEsRepository strategyEsRepository;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // TODO: 2024/8/12 接收消息
    @RabbitListener(queues = {"esQueue"})
    public void onEsMessage(String message){
//        int a = 10 / 0;
        Strategy strategy = JSON.parseObject(message, Strategy.class);
        StrategyEs es = new StrategyEs();
        BeanUtils.copyProperties(strategy,es);
        strategyEsRepository.save(es);
        String key = RedisKeys.ES_DATA.join("key");
        stringRedisTemplate.opsForZSet().remove(key,message);
    }

}
